﻿using CanDichVU.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CanDichVU.Controllers
{
    [Authorize]
    public class UploadController : Controller
    {
        //
        // GET: /Upload/

        public ActionResult BrowseImage(string CKEditorFuncNum, string CKEditor, string langCode)
        {
            String[] exts = { "*.png", "*.gif", "*.jpg", "*.bmp" };
            var vFolderPath = Server.MapPath("/Content/uploads/");
            var vBrowseViewModel = new AdminBrowseImageViewModel { CKEditorFuncNum = CKEditorFuncNum };
            vBrowseViewModel.ListForder = new System.Collections.Generic.List<ListForder>();
            if (Directory.Exists(vFolderPath))
            {
                foreach (var ext in exts)
                {
                    var listForder = Directory.GetFiles(vFolderPath, ext);
                    foreach (var fd in listForder)
                    {
                        ListForder ml = new ListForder();
                        DirectoryInfo info = new DirectoryInfo(fd);
                        ml.name = info.Name;
                        ml.Path = "/Content/uploads/" + Path.GetFileName(fd);
                        vBrowseViewModel.ListForder.Add(ml);
                    }
                }

                // vBrowseViewModel.ImagePaths = Directory.GetFiles(vFolderPath).Select(p => Url.Content("/Content/uploads/" + Path.GetFileName(p))).ToList();
            }

            return View(vBrowseViewModel);
        }

        public ActionResult BrowseFindImageOnPath(string Paths)
        {
            if (string.IsNullOrEmpty(Paths))
            {
                Paths = "/Content/uploads/";
            }
            String[] exts = { "*.png", "*.gif", "*.jpg", "*.bmp" };
            var vFolderPath = Server.MapPath(Paths);
            var vBrowseViewModel = new AdminBrowseImageViewModel { CKEditorFuncNum = "1" };
            vBrowseViewModel.ListForder = new System.Collections.Generic.List<ListForder>();
            if (Directory.Exists(vFolderPath))
            {
                foreach (var ext in exts)
                {
                    var listForder = Directory.GetFiles(vFolderPath, ext);
                    foreach (var fd in listForder)
                    {
                        ListForder ml = new ListForder();
                        DirectoryInfo info = new DirectoryInfo(fd);
                        ml.name = info.Name;
                        ml.Path = Paths + Path.GetFileName(fd);
                        vBrowseViewModel.ListForder.Add(ml);
                    }
                }

                // vBrowseViewModel.ImagePaths = Directory.GetFiles(vFolderPath).Select(p => Url.Content("/Content/uploads/" + Path.GetFileName(p))).ToList();
            }

            return PartialView("_BrowseFindImageOnPath", vBrowseViewModel);
        }


        public ActionResult ListFolderTreeView(string forder)
        {
            List<ListForder> list = new List<ListForder>();

            var vFolderPath = Server.MapPath(forder);

            var listForder = Directory.GetDirectories(vFolderPath);
            foreach (var fd in listForder)
            {
                ListForder ml = new ListForder();
                DirectoryInfo info = new DirectoryInfo(fd);
                ml.name = info.Name;
                ml.Path = forder + info.Name + "/";
                list.Add(ml);
            }

            return PartialView("_ListFolderTreeView", list);

        }

    }
}
